
import { request } from "../../utils/request";
import config from "../../utils/config";

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        picUrl: config.picUrl,
        custom: wx.getMenuButtonBoundingClientRect(),
        user:wx.getStorageSync('userInfo'),
        img:[],
        content:'',
        formData: {album_id: 1},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    uploadBanner() {
        this.upload(5 - this.data.img.length, res => {
            var img = this.data.img
            img.push(res.data.src)
            this.setData({img})
        })
    },
    upload(count, callback) {
        wx.showLoading()
        wx.chooseImage({
            count: count,
            sizeType: ['compressed', 'original'],
            success: res => {
                for (let v of res.tempFiles) {
                    wx.uploadFile({
                        url: config.url + '/system/api/uploadImage',
                        filePath: v.path,
                        name: 'file',
                        formData: this.data.formData,
                        success: res => {
                            wx.hideLoading()
                            callback(JSON.parse(res.data))
                        }
                    })
                }
            },
            fail: err => {
                wx.hideLoading()
            }
        })
    },
    changeValue(e) {
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]: value})
    },
    submit(){
        var user = wx.getStorageSync('userInfo')
        request({
            url:"/miniprogram/user/addFeedback",
            data:{content:this.data.content,img:this.data.img.join(','),user_id:user.id},
            type:'post'
        }).then(res=>{
            if(res.code != 200){
                wx.showToast({title:res.msg,icon:'none'})
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none',
                  success:res=>{
                      setTimeout(()=>{
                        wx.navigateBack({
                          delta: 1,
                        })
                      },1500)
                  }
                })
            }
        })
    }
})